
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

namespace eCom
{
	class eComPricingBizRules : Syrinx.BaseBizRules
	{
		public IBizObjList getQuantityDiscounts(IApplication app, IRuleContext context, Swaf.BGuid.Guid companyId)
		{
			context.put("companyId", companyId==null?ActiveCompanyId:companyId);
			IBizObjList pricing = app.appData.get("DA.eCom.GetCompanyQuantityDiscounts", context) as IBizObjList;
			return pricing;
		}

		public void deleteQuantityDiscount(IApplication app, IRuleContext context, Swaf.BGuid.Guid pricingId)
		{
			context.put("id", pricingId);
			app.appData.put("DA.eCom.DeleteCompanyQuantityDiscount", context);
		}

		public void updateQuantityDiscount(IApplication app, IRuleContext context, IBizObj pricing)
		{
			if (pricing.isNew)
			{
				pricing.put("companyId", ActiveCompanyId);
				app.appData.put("DA.eCom.AddCompanyQuantityDiscount", pricing);
			}
			else
				app.appData.put("DA.eCom.UpdateCompanyQuantityDiscount", pricing);
		}

		public IBizObjList getProductQuantityDiscounts(IApplication app, IRuleContext context, Swaf.BGuid.Guid productId)
		{
			context.put("productId", productId);
			IBizObjList pricings = app.appData.get("DA.eCom.GetProductQuantityDiscounts", context) as IBizObjList;
			return pricings;
		}

		public void setProductQuantityDiscounts(IApplication app, IRuleContext context, Swaf.BGuid.Guid productId, IBizObjList pricingsToRemove, IBizObjList pricingsToAdd)
		{
			try
			{
				if (productId != null)
				{
					context.put("productId", productId);
					if (pricingsToRemove != null)
						foreach (IBizObj pricing in pricingsToRemove)
						{
							context.put("pricingId", pricing.get("id"));
							app.appData.put("DA.eCom.DeleteProductQuantityDiscount", context);
						}
					if (pricingsToAdd != null)
						foreach (IBizObj pricing in pricingsToAdd)
						{
							context.put("pricingId", pricing.get("id"));
							app.appData.put("DA.eCom.AddProductQuantityDiscount", context);
						}
				}
			}
			catch (Exception e)
			{
				throw new eComException("Error executing setProductpricing.", e);
			}
		}

		public IBizObj updateSalesOrderItemQuantityDiscounts(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			IBizObj orderItem = resultValue as IBizObj;
			if (orderItem != null)
			{
				int orderItemQuantity = (int)orderItem.get("quantity");
				Decimal price = (Decimal)orderItem.get("price");
				IBizObjList pricings = context.RuleGroup.execute(context, "getProductQuantityDiscounts", orderItem.get("product.id")).CallResults as IBizObjList;
				if (pricings != null)
					foreach (IBizObj pricing in pricings)
					{
						int pricingQuantity = (int)pricing.get("quantity");

						//Hack code - this should somehow be moved to reservation rule...
						//Need to get this drop to first client now, so we put it here for ease.
						IResults rc = context.RuleGroup.execute(context, "getCurrentSalesOrderDetails");
						IBizObj salesOrder = rc.CallResults as IBizObj;
						if (salesOrder.containsField("reservation"))
						{
							orderItemQuantity = (int)salesOrder.get("reservation.numDaysStay");
							price = (Decimal)salesOrder.get("reservation.normalNightsTotal");
						}

						if (orderItemQuantity >= pricingQuantity)
						{
							Decimal baseDiscount = (Decimal)pricing.get("baseDiscount");
							Decimal percentDiscount = (Decimal)pricing.get("percentCost");
							Decimal newPrice = price;
							newPrice -= baseDiscount;
							newPrice -= newPrice * (percentDiscount / 100);
							Decimal savings = (decimal)(price - newPrice);
							price = (Decimal)orderItem.get("price");
							orderItem.put("price", price-savings);
							string desc = String.Format("{2}<br/>{0} applied for savings of {1}", pricing.get("name"), savings.ToString("N"), orderItem.get("description").ToString());
							orderItem.put("description", desc);
							app.appData.put("DA.eCom.UpdateSalesOrderItem", orderItem);
							break;
						}
					}
			}
			return orderItem;
		}
	}
}
